package com.itstherules.jmsunit.mock;

import org.junit.Assert;
import org.junit.Test;

import com.itstherules.jmsunit.expectation.AlwaysGoodExpectation;
import com.itstherules.jmsunit.expectation.JmsHeadersDefinitionImpl;
import com.itstherules.jmsunit.expectation.TextEqualsExpectation;
import com.itstherules.jmsunit.headers.JmsHeadersDefinition;
import com.itstherules.jmsunit.mock.store.Store;

public class StoreTest {

	final String separator = System.getProperty("line.separator");

	@Test
	public void canStoreThroughExpectation() {
		Store unit = new Store();
		Store reply = unit.through("vm://green");
		Assert.assertEquals("Expectation: " + separator + "through: vm://green"
				+ separator + "with: The Expectations are: empty" + separator, reply.toString());
		Assert.assertSame(unit, reply);
	}

	@Test
	public void canStoreExpectedCondition() {
		Store unit = new Store();
		Store reply = unit.with(new TextEqualsExpectation("fred"));
		Assert.assertEquals("Expectation: " + separator+
				"with: The Expectations are: [Text is equals (expected is fred)]" + separator, reply.toString());
		Assert.assertSame(unit, reply);
	}


	@Test
	public void canStore2ExpectedConditions() {
		Store unit = new Store();
		Store reply = unit.with(new TextEqualsExpectation("fred")).with(new AlwaysGoodExpectation());
		Assert.assertEquals("Expectation: " + separator+ 
				"with: The Expectations are: [Text is equals (expected is fred)][Expected to be always good]" + separator, 
				reply.toString());
		Assert.assertSame(unit, reply);
	}
	
	@Test
	public void canStoreReply() {
		JmsHeadersDefinition yellow = new JmsHeadersDefinitionImpl("YellowEndPoint");
		yellow.setHeader("ApplOriginData", "YELLOW");
		yellow.setHeader("Version", "1.0");
		yellow.setHeader("JMS_IBM_MsgType", "1");

		Store unit = new Store();
		Store reply = unit.reply("vm://blue", "message", 
				yellow);
		Assert
				.assertEquals(
						"Expectation: "
								+ separator
								+ "with: The Expectations are: empty" 
								+ separator
								+ "reply:"
								+ separator
								+ "	with: message"
								+ separator
								+ "	to: queue vm://blue"
								+ separator
								+ "	pretending to be: YellowEndPoint (ApplOriginData=YELLOW&Version=1.0&JMS_IBM_MsgType=1)"
								+ separator, reply.toString());
		Assert.assertSame(unit, reply);
	}
	
	
	
	@Test
	public void canWriteAFullStore(){
		JmsHeadersDefinition yellow = new JmsHeadersDefinitionImpl("YellowEndPoint");
		yellow.setHeader("ApplOriginData", "YELLOW");
		yellow.setHeader("Version", "1.0");
		yellow.setHeader("JMS_IBM_MsgType", "1");

		Store unit = new Store();
		
		Store reply = unit.through("vm://blue").with(new TextEqualsExpectation("redMessage")).reply("vm://green", "yellowMessage", yellow);
		Assert.assertEquals("Expectation: "+separator+ 
				"through: vm://blue"+separator+ 
				"with: The Expectations are: [Text is equals (expected is redMessage)]"+separator+ 
				"reply:"+separator+ 
				"	with: yellowMessage"+separator+ 
				"	to: queue vm://green"+separator+ 
				"	pretending to be: YellowEndPoint (ApplOriginData=YELLOW&Version=1.0&JMS_IBM_MsgType=1)"+separator+ 
				"", reply.toString());
	}
	
	@Test
	public void requiresAReply(){
		JmsHeadersDefinition red = new JmsHeadersDefinitionImpl("Red");
		Store unit = new Store();
		unit.reply(null, null, null);
		Assert.assertFalse(unit.requiresAReply());
		unit.reply(null, null, red);
		Assert.assertFalse(unit.requiresAReply());
		unit.reply(null, "", red);
		Assert.assertFalse(unit.requiresAReply());
		unit.reply("", "", red);
		Assert.assertTrue(unit.requiresAReply());
	}

	@Test(expected=AssertionError.class)
	public void getBroadcastInfoThrows(){
		Store unit = new Store();
		unit.getSendReplyInformation();
	}

}
